package ihm.overlays;

import java.util.ArrayList;

import ihm.widgets.Element;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;

public class ListElementsOverlay extends Composite {

	private final ArrayList<ElementListener> elementsListener = new ArrayList<ElementListener>();
	private final ArrayList<Element> userWidgets = new ArrayList<Element>();
	
	public ListElementsOverlay(Composite father) {
		super(father, SWT.NONE);

		// init layout
		RowLayout layout = new RowLayout();
		layout.wrap = true;
		layout.justify = false;
		layout.pack = true;
		setLayout(layout);
	}

	/**
	 * add an Element in the ui list.
	 * 
	 * @param caption
	 *            displayed under the picture
	 * @param picture
	 */
	public void addElement(String caption, Image picture) {
		final Element u = new Element(this, caption, picture);
		this.userWidgets.add(u);
		u.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent event) {
				for (ElementListener fl : elementsListener) {
					fl.elementClicked(u);
				}
			}
		});
		
		layout();
	}

	public void addElementListener(ElementListener listener) {
		elementsListener.add(listener);
	}

	/**
	 * clear all elements in the the ui list.
	 */
	public void clear() {

		for(Element e : this.userWidgets){
			e.dispose();
		}
		this.userWidgets.clear();
		layout(true);
	}

}
